package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.Emp;
import com.itheima.pojo.PageBean;
import com.itheima.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class EmpServiceImpl implements EmpService {
    @Override
    public Emp login(Emp emp2) {
        return empMapper0.getByUsernameAndPassword(emp2);
    }

    @Autowired
    private EmpMapper empMapper0;

//    @Override
//    public PageBean page(Integer page, Integer pageSize) {
//        //1.获取总记录数
//        Long count=empMapper0.count();
//        //2.获取当前页数据列表
//        Integer start=(page-1)*pageSize;
//        List<Emp> empList=empMapper0.page(start,pageSize);
//        //3.上面两个封装成一个PageBean
//        PageBean pageBean=new PageBean(count,empList);
//        return pageBean;//返回给controller层
//    }

    /**
     * 用pagehelper插件
     */
    public PageBean page(Integer page, Integer pageSize, String name, Short gender,
                         LocalDate begin, LocalDate end){
        PageHelper.startPage(page,pageSize);
        List<Emp> empList=empMapper0.page(name,gender, begin,end);//不用传参了,但要转类型
        Page<Emp> page1=(Page<Emp>) empList;
        PageBean pageBean=new PageBean(page1.getTotal(),page1.getResult());
        return pageBean;
    }

    /**
     * 根据多个id批量删除
     * @param ids
     */
    @Override
    public void delete(Integer[] ids) {
        empMapper0.deleteByIds(ids);
    }

    /**
     * 新增员工
     * @param emp0
     */
    @Override
    public void add(Emp emp0) {
        emp0.setCreateTime(LocalDateTime.now());//因为前端没有传这些数据库中字段，所以要补充这些属性，否则数据库会报错
        emp0.setUpdateTime(LocalDateTime.now());
        empMapper0.addemp(emp0);
    }
}
